package example.双指针;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

// https://leetcode-cn.com/problems/remove-element/
public class Demo2 {
    public static int removeElement(int[] nums, int val) {
        //min--->
        int left = 0;
        //<---max
        int right = nums.length;
        while (left < right) {
            if (nums[left] == val) {
                nums[left] = nums[right - 1];
                right--;
            } else {
                left++;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int[] nums = {0, 1, 2, 2, 3, 0, 4, 2};
        System.out.println(removeElement(nums, 2));
        System.out.println(Arrays.toString(nums));
        int[] nums2 = {3, 2, 2, 3};
        System.out.println(removeElement(nums2, 3));
        System.out.println(Arrays.toString(nums2));
    }
}
